//
//  DWUserInfo.swift
//  DWater-Swift
//
//  Created by mac on 2017/4/18.
//  Copyright © 2017年 Dimon.胡亚宾. All rights reserved.
//

import UIKit

class DWUserInfo: NSObject {
    
    var userId       : String = ""
    var token        : String = ""
    var userName     : String = ""
    var phoneNumeber : String = ""

   open class func parseFormDict(dict:Dictionary<String, Any>) -> DWUserInfo? {

        guard let _ = dict["yhid"]  ,
              let _ = dict["token"]   else { return nil}

        let info = DWUserInfo();
        let yhid = dict["yhid"]!
    
        info.userId         = "\(yhid)"
        info.token          = (dict["token"] as? String)!
        info.userName       = (dict["yhmc"] as? String)!
        info.phoneNumeber   = (dict["yhzh"] as? String)!
        return info
    }
    
   open class func getFromLocalFile() -> DWUserInfo! {
     
    guard  let _ = defaults.object(forKey: kSaveMacroUserID),
           let _ = defaults.object(forKey: kSaveMacroUserToken),
           let _ = defaults.object(forKey: kSaveMacroUserName),
           let _ = defaults.object(forKey: kSaveMacroUserPhone)
        else {
          return nil
        }
    
    let info = DWUserInfo();
    info.userId   = defaults.object(forKey: kSaveMacroUserID) as! String!
    info.token    = (defaults.object(forKey: kSaveMacroUserToken) as! String?)!
    info.userName = (defaults.object(forKey: kSaveMacroUserName) as! String?)!
    info.phoneNumeber = (defaults.object(forKey: kSaveMacroUserPhone) as! String?)!
    return info
    
    }
    
   open class func saveToLocalFile(userInfo:DWUserInfo?) {

    if (userInfo != nil) {
       
        defaults.setValue(userInfo?.userId, forKey: kSaveMacroUserID)
        defaults.setValue(userInfo?.token, forKey: kSaveMacroUserToken)
        defaults.setValue(userInfo?.userName, forKey: kSaveMacroUserName)
        defaults.setValue(userInfo?.phoneNumeber, forKey: kSaveMacroUserPhone)
        defaults.synchronize()
    
    }else{

        defaults.removeObject(forKey: kSaveMacroUserID)
        defaults.removeObject(forKey: kSaveMacroUserToken)
        defaults.removeObject(forKey: kSaveMacroUserName)
        defaults.removeObject(forKey: kSaveMacroUserPhone)
        defaults.synchronize()
            
        }
    }
}
